package com.unicom.mec.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unicom.mec.user.common.ResultBean;
import com.unicom.mec.user.mapper.UserMapper;
import com.unicom.mec.user.model.User;
import com.unicom.mec.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ZhuHao
 * @since 2020-07-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public String register(UserVo userVo) {
        // 查询数据库是否有重复的账号
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getAccount,userVo.getAccount());
        User result = userMapper.selectOne(queryWrapper);
        if (!Objects.isNull(result)) {
            return ResultBean.FAIL;
        }
        // 进行数据库插入操作
        Long createId = 1271322350203121664L;
        Date now = new Date();
        Long id = IdWorker.getId();
        User user = new User();
        BeanUtils.copyProperties(userVo,user);
        user.setId(id);
        user.setUserType(3);
        user.setCreateId(createId);
        user.setCreateTime(now);
        user.setUpdateId(createId);
        user.setUpdateTime(now);
        userMapper.insert(user);
        return ResultBean.SUCCESS;
    }
}
